package com.yikome.promotion.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;
import com.itmuch.lightsecurity.annotation.PreAuthorize;
import com.yikome.promotion.entity.Coupon;
import com.yikome.promotion.request.CouponRequest;
import com.yikome.promotion.response.CouponResponse;
import com.yikome.promotion.service.CouponService;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 每满减活动控制器
 * 新增/修改, 删除, 下线, 上线, 查询
 */

@Slf4j
@RestController
@RequestMapping("/admin/promotion/coupon")
public class CouponController {

	@Autowired
	private CouponService couponService ; 
	
	
	@GetMapping("/list")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop')")
	public Page<CouponResponse> findFullReduction(CouponRequest couponRequest , @PageableDefault(sort = {"pro_coupon.id"}, direction = Sort.Direction.DESC)Pageable pageable){
		
		return couponService.findCoupon(couponRequest, pageable);
	}
	
	@PostMapping
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop')")
	public Coupon persistFullReduction(@RequestBody Coupon couponRequest) {
		
		Preconditions.checkArgument(couponRequest.getShopId() != null, "参数不全,商城ID不存在....");
		Preconditions.checkArgument(StrUtil.isNotBlank(couponRequest.getPromotionName()), "参数不全,活动名称不存在....");
		Preconditions.checkArgument(couponRequest.getReductionValue() != null, "参数不全,[减]数值不存在....");
		
		Preconditions.checkArgument(couponRequest.getBegainTime() != null, "参数不全,开始时间不存在....");
		Preconditions.checkArgument(couponRequest.getEndTime() != null, "参数不全,结束时间不存在....");
		
		Date nowTime = Calendar.getInstance(TimeZone.getTimeZone("+8")).getTime();
		Preconditions.checkArgument(couponRequest.getEndTime().after(couponRequest.getBegainTime()) && nowTime.before(couponRequest.getEndTime()) , "参数错误,时间范围不正确...");
		
		return couponService.persistCoupon(couponRequest);
	}
	
	
	@DeleteMapping("/{couponId}")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop')")
	public Boolean deleteCoupon(@PathVariable("couponId") Long couponId) {
		
		couponService.deleteCoupon(couponId);
		
		return Boolean.TRUE;
	}
	
	@PutMapping("/{couponId}/up")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop')")
	public Boolean upFullCoupon(@PathVariable("couponId") Long couponId) {
		
		couponService.upFullCoupon(couponId);
		
		return Boolean.TRUE ;
	}
	
	@PutMapping("/{couponId}/down")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop')")
	public Boolean downCoupon(@PathVariable("couponId") Long couponId) {
		
		couponService.downCoupon(couponId);
		
		return Boolean.TRUE ;
	}
}
